package framework.view.components.container
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;

import framework.core.ComponentBuilder;
import framework.proxy.ComponentProxy;
import framework.proxy.IComponentProxy;

/**
 * RepeaterProxy
 * @author jliang
 * 
 */
public class RepeaterProxy extends ComponentProxy
{
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function RepeaterProxy()
    {
        super();
    }
    //==========================================================================
    //  Properties
    //==========================================================================
    protected var _items:Vector.<DisplayObject>;

    public function get items():Vector.<DisplayObject>
    {
        return _items;
    }
    
    protected var _itemProxys:Vector.<IRenderProxy>;
    
    public function get itemProxys():Vector.<IRenderProxy>
    {
        return _itemProxys;
    }
    
    protected var _data:*;
    
    public function set data(value:*):void
    {
        _data = value;
        if (renderProxyRef)
        {
            var index:int = 0;
            var proxy:IRenderProxy;
            for each (var renderData:* in data)
            {
				if (index >= itemProxys.length) break;
                proxy = itemProxys[index];
                if (proxy) 
                {
                    proxy.data = renderData;
                    proxy.show();
                }
                else
                {
                    break;
                }
                index ++;
            }
            
            if (itemProxys.length > index)
            {
                for (var i:int = index; i < itemProxys.length; i++)
                {
                    proxy = itemProxys[i];
                    proxy.hide();
                }
            }
        }
    }
    
    public function get data():*        
    {
        return _data;
    }
    
    protected var _multipleSelection:Boolean = false;

    public function get multipleSelection():Boolean
    {
        return _multipleSelection;
    }

    public function set multipleSelection(value:Boolean):void
    {
        _multipleSelection = value;
    }
    
    protected var _onItemSelected:Function;
	
	/**
	 *  function(render:IRenderProxy)
	 * @return 
	 * 
	 */	
    public function get onItemSelected():Function
    {
        return _onItemSelected;
    }
    
    /**
     *  function(render:DisplayObject):void;
     * 
     * @param value
     * 
     */    
    public function set onItemSelected(value:Function):void
    {
        _onItemSelected = value;
    }
    
    protected var _renderProxyRef:Class;    

    public function get renderProxyRef():Class
    {
        return _renderProxyRef;
    }
    
    /**
     * IRenderProxy 
     * @param value
     * 
     */    
    public function set renderProxyRef(value:Class):void
    {
        _renderProxyRef = value;
    }
	
	protected var _selectedIndex:int = -1;

	public function get selectedIndex():int
	{
		return _selectedIndex;
	}

	public function set selectedIndex(value:int):void
	{
		_selectedIndex = value;
		
		items[selectedIndex].dispatchEvent(new Event(Event.SELECT));
	}
    //==========================================================================
    //  Overridden methods: ComponentProxy
    //==========================================================================
    override public function parse(target:MovieClip):void
    {
        super.parse(target);
        
        _items = new Vector.<DisplayObject>();
        _itemProxys = new Vector.<IRenderProxy>();
        
        for (var i:int = 0; i < skin.numChildren; i ++)
        {
            var child:DisplayObject = skin.getChildAt(i);
            if (child.name.indexOf("_render_") > 0)
            {
                var index:int = int(child.name.split("_render_")[1]);
                items[index] = child;
                child.addEventListener(Event.SELECT, itemSelectedHandler);
                
                if (renderProxyRef && child is MovieClip)
                {
                    var proxy:IRenderProxy = ComponentBuilder.create(renderProxyRef);
                    proxy.parse(MovieClip(child));
                    itemProxys[index] = proxy;
                }
            }
        }
    }
    
    override public function dispose():void
    {
        for (var i:int = 0; i < items.length; i++)
        {
            var child:DisplayObject = items[i];
            child.removeEventListener(Event.SELECT, itemSelectedHandler);
            
            if (itemProxys[i])
            {
                IComponentProxy(itemProxys[i]).dispose();
            }
        }
    }
    //==========================================================================
    //  Event handlers
    //==========================================================================
    protected function itemSelectedHandler(event:Event):void
    {
		var selectedProxy:IRenderProxy;
		
		for (var i:int = 0; i < items.length; i++)
		{
			if (items[i] == event.target)
			{
				selectedProxy = itemProxys[i];
				_selectedIndex = i;
				selectedProxy.selected = true;
			}
			else
			{
				itemProxys[i].selected = false;
			}
		}
		
        if (onItemSelected != null)
        {
            onItemSelected(selectedProxy);
        }
    }
}
}

